<template>
  <a-modal :title="$t('账户汇总')" :visible="visible" @cancel="handleCancel" width="50%" class="modal_title" v-drag>
    <template slot="footer">
      <a-button @click="handleCancel">{{ $t('关闭') }}</a-button>
    </template>
    <div>
      <a-row style="margin-bottom: 10px">
        <a-col :span="24" style="text-align: right">
          <a-button v-has="'MemberInfoDrawingController:export'"
          type="primary" :loading="exloading" @click="handleExportXls($t('账户汇总'))" style="margin-right: 8px">{{
            $t('导出')
          }}</a-button>
          <a-button v-has="'MemberInfoDrawingController:print'"
          type="primary" v-print="'#printme'">{{ $t('打印') }}</a-button>
        </a-col>
      </a-row>
      <div id="printme">
        <a-table
          ref="table"
          size="middle"
          bordered
          :columns="columns"
          :dataSource="dataSource"
          :pagination="pagination"
          class="j-table-force-nowrap"
        ></a-table>
      </div>
    </div>
  </a-modal>
</template>

<script>
import { getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
export default {
  name: 'AccountSumModal',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      visible: false,
      columns: [
        {
          title: this.$t('会员类型'),
          align: 'center',
          dataIndex: 'memberType_dictText',
        },
        {
          title: this.$t('会员数量'),
          align: 'center',
          dataIndex: 'count',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('代币'),
          align: 'center',
          dataIndex: 'coin',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('积分'),
          align: 'center',
          dataIndex: 'integral',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('彩票'),
          align: 'center',
          dataIndex: 'lottery',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('A票'),
          align: 'center',
          dataIndex: 'alottery',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('A币'),
          align: 'center',
          dataIndex: 'points',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
      ],
      pagination: false,
      dataSource: [],
      queryParam: {},
      isCustom: true,
      url: {
        exportXlsUrl: '/memberInfoDrawing/totalByTypeExportXls',
      },
    }
  },
  created() {},
  methods: {
    handleCancel() {
      this.visible = false
    },
    edit(record) {
      this.visible = true
      this.infoData = record
      this.queryParam = Object.assign({}, record)
      getAction('/memberInfoDrawing/totalByType', this.queryParam).then((res) => {
        this.dataSource = res.result
        this.dataSource.forEach((col) => {
          col.coin = (Number(col.realCoin) + Number(col.presentCoin))
        })
        if (this.dataSource.length > 0) {
          this.addSumRow(
            ['memberType_dictText', 'count', 'coin', 'integral', 'lottery', 'alottery', 'points'],
            this.dataSource
          )
        }
      })
    },
    addSumRow(myArr, dataSource) {
      let that = this,
        total = {}
      myArr.forEach(function (tcn, ind) {
        if (ind == 0) {
          total[tcn] = that.$t('合计')
        } else {
          total[tcn] = 0
        }
      })
      for (let key in total) {
        dataSource.forEach(function (ds) {
          if (!isNaN(total[key])) {
            total[key] += Number(ds[key])
          }
        })
        if (key == 'coin') {
          total[key] += total[key]
        }
      }
      dataSource.push(total)
    },
  },
}
</script>
<style>
.black-row {
  margin: 0 15px;
  border: 1px solid #d9d9d9;
  padding: 10px;
  border-radius: 5px;
  font-size: 15px;
}
.top-row {
  margin: 10px;
}
</style>